package com.jvm123.api.api.controller

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.client.RestTemplate

/**
 *
 * @author yawn http://jvm123.com
 *     2019/11/11 14:50
 */
@RestController
class BingPicController {

    @Value('${bing.domain}')
    String domain = "https://www.bing.com"

    String picUri = "/HPImageArchive.aspx?format=js&n=1&idx="

    @Autowired
    RestTemplate restTemplate

    @GetMapping("bing/{idx}")
    Map bing(@PathVariable(required = false) int idx) {
        ResponseEntity<Map> res = restTemplate.getForEntity(domain + picUri + idx, Map.class)
        Map body = res.body
        List<Map> imgList = body["images"]
        Map<String, String> img = imgList[0]
        String url = img["url"]
        img.put("fullUrl", domain + url)
        img
    }

}
